<?php
require_once 'abstractModel.php';

class BoardModel extends AbstractModel{
	//TABLE NAMES
	const TABLE_NAME_MAIN_BOARD = 'tMainBoard';
	const TABLE_NAME_MAIN_BOARD_RESPONSE = 'tMainBoardResponse';
	const TABLE_NAME_MAIN_BOARD_LIKE = 'tMainBoardLike';

	//MAIN_BOARD
	const MAIN_BOARD_INDEX = 'MBIndex';
	const MAIN_BOARD_USER_ID = 'userId';
	const MAIN_BOARD_TITLE = 'MBTitle';
	const MAIN_BOARD_CREATED_DATE = 'MBCreatedDate';
	const MAIN_BOARD_REFRESHED_DATE = 'MBRefreshDate';
	const MAIN_BOARD_CONTENT = 'MBContent';

	//MAIN_BOARD_RESPONSE
	const MAIN_BOARD_RESPONSE_INDEX = 'MBRIndex';
	const MAIN_BOARD_RESPONSE_MAIN_BOARD_INDEX = 'MBIndex';
	const MAIN_BOARD_RESPONSE_USER_ID = 'userId';
	const MAIN_BOARD_RESPONSE_CREATED_DATE = 'MBRCreatedDate';
	const MAIN_BOARD_RESPONSE_REFRESHED_DATE = 'MBRefreshedDate';
	const MAIN_BOARD_RESPONSE_CONTENT = 'MBRContent';

	//MAIN_BOARD_LIKE
	const MAIN_BOARD_LIKE_INDEX = 'MBLIndex';
	const MAIN_BOARD_LIKE_MAIN_BOARD_INDEX = 'MBIndex';
	const MAIN_BOARD_LIKE_USER_ID = 'userId';

	public function __construct(){
		parent::__construct();
		$this->targetTable=self::TABLE_NAME_MAIN_BOARD;
	}

	/**
	 * @name setTargetTable
	 * @param $tableName 下の３つが OPTIONAL
	 * @param BoardModel::TABLE_NAME_MAIN_BOARD
	 * @param BoardModel::TABLE_NAME_MAIN_BOARD_RESPONSE
	 * @param BoardModel::TABLE_NAME_MAIN_BOARD_LIKE
	 */

	public function setTargetTable($targetTable){
		if ($targetTable==self::TABLE_NAME_MAIN_BOARD ||
			$targetTable==self::TABLE_NAME_MAIN_BOARD_LIKE ||
			$targetTable==self::TABLE_NAME_MAIN_BOARD_RESPONSE)
			$this->targetTable=$targetTable;
	}

	public function insert(){
		
		switch ($this->targetTable) {
			case self::TABLE_NAME_MAIN_BOARD:
				$userId = func_get_arg(0);
				$mbTitle = func_get_arg(1);
				$mbContent = func_get_arg(2);
				$sth = $this->db->prepare("Insert into {$this->targetTable}(userId, MBTitle, MBContent, MBRefreshDate) values(?, ?, ?, NOW())");
				$sth->bindParam(1, $userId);
				$sth->bindParam(2, $mbTitle);
				$sth->bindParam(3, $mbContent);
				break;

			case self::TABLE_NAME_MAIN_BOARD_LIKE:
				$mbIndex = func_get_arg(0);
				$userId = func_get_arg(1);
				$sth = $this->db->prepare("select * from {$this->targetTable} where userId=?");
				$sth->bindParam(1, $userId);
				$result = $this->select($sth);
				if($result[0][self::MAIN_BOARD_LIKE_USER_ID]!=$userId){
					$this->select($sth);
					$sth = $this->db->prepare("Insert into {$this->targetTable}(MBIndex, userId) values(?, ?)");
					$sth->bindParam(1, $mbIndex);
					$sth->bindParam(2, $userId);
				} else {
					return;
				}
				break;

			case self::TABLE_NAME_MAIN_BOARD_RESPONSE:
				$mbIndex = func_get_arg(0);
				$userId = func_get_arg(1);
				$mbrContent = func_get_arg(2);
				$sth = $this->db->prepare("Insert into {$this->targetTable}(MBIndex, userId, MBRContent, MBRefreshedDate) values(?, ?, ?, NOW())");
				$sth->bindParam(1, $mbIndex);
				$sth->bindParam(2, $userId);
				$sth->bindParam(3, $mbrContent);
				break;
		}
		return $sth->execute();
	}

	public function select($sth){
		return parent::select($sth);
	}

	public function update(){
		switch ($this->targetTable) {
			case self::TABLE_NAME_MAIN_BOARD:
				//$sth = $this->db->prepare("Update {$this->targetTable} SET MBTitle=?, MBContent=?, MBRefreshDate=NOW()");
				$mbTitle = func_get_arg(0);
				$mbContent = func_get_arg(1);
				$mbIndex = func_get_arg(2);
				$sth = $this->db->prepare("Update {$this->targetTable} SET MBTitle=?, MBContent=?, MBRefreshDate=NOW() where MBIndex = ?");
				$sth->bindParam(1, $mbTitle);
				$sth->bindParam(2, $mbContent);
				$sth->bindParam(3, $mbIndex);
				break;

			case self::TABLE_NAME_MAIN_BOARD_RESPONSE:
				$mbrContent = func_get_arg(0);
				$mbrRefreshedDate = func_get_arg(1);
				$mbrIndex = func_get_arg(2);
				$sth = $this->db->prepare("Update {$this->targetTable} SET MBRContent=?, MBRefreshedDate=NOW() where MBRIndex=?");
				$sth->bindParam(1, $mbrContent);
				$sth->bindParam(2, $mbrRefreshedDate);
				$sth->bindParam(3, $mbrIndex);
				break;
		}
		return $sth->execute();
	}
	public function delete($where){
		if ($this->targetTable==self::TABLE_NAME_MAIN_BOARD) {
			$tableIndex = self::MAIN_BOARD_INDEX;
		} else if ($this->targetTable==self::TABLE_NAME_MAIN_BOARD_LIKE) {
			$tableIndex = self::MAIN_BOARD_LIKE_INDEX;
		} else {
			$tableIndex = self::MAIN_BOARD_RESPONSE_INDEX;
		}
		$sth = $this->db->prepare("Delete from {$this->targetTable} where {$tableIndex} = ?");
		$sth->bindParam(1, $where);
		return $sth->execute();
	}
}